package com.ruoyi.web.controller.system;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.system.domain.Project;
import com.ruoyi.system.domain.ProjectProperty;
import com.ruoyi.system.service.IProjectPropertyService;
import com.ruoyi.system.service.IProjectService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/properties")
public class PropertyController extends BaseController {

    @Autowired
    private IProjectService projectService;

    @Autowired
    private IProjectPropertyService projectPropertyService;

    /***
     * 根据环境获取不同的配置文件
     */
    @RequestMapping(value = "/{projectName}/{env}/resource.properties")
    public void projectProperty(@PathVariable("projectName") String projectName, @PathVariable("env") String env, HttpServletResponse response) {
        Project project = projectService.selectProjectByName(projectName);
        if (project == null) {
            logger.warn("【项目名称不存在，projectName = " + projectName + "】");
            return;
        }
        List<ProjectProperty> projectProperties = projectPropertyService.selectProjectPropertyList(null);
        if (CollectionUtils.isEmpty(projectProperties)) {
            logger.warn("【该项目未配置任何属性值，projectName = " + projectName + "】");
            return;

        }
        String result = projectProperties.stream().filter(p -> project.getId() == p.getProjectId()).map(p -> {
            switch (env) {
                case "dev":
                    return p.getKey() + "=" + p.getDevValue() + "\n";
                case "test":
                    return p.getKey() + "=" + p.getTestValue() + "\n";
                case "vt":
                    return p.getKey() + "=" + p.getVtValue() + "\n";
                default:
                    logger.warn("【非法的evn值，env = " + env + "】");
                    return null;
            }

        }).collect(Collectors.joining("\n"));

        logger.info("【返回的值为：" + result + "】");

//        String basePath = "D:\\nginx-1.16.1\\html";
//        String filePath = basePath + "\\" + projectName + "\\" + env + "\\" + "application.properties";
//        String fileFolder = basePath + "\\" + projectName + "\\" + env + "\\";
//        File folder = new File(fileFolder);
//        if (!folder.exists()) {
//            folder.mkdirs();
//        }
//        File file = new File(filePath);
//        file.deleteOnExit();
//        try {
//            file.createNewFile();
//            FileUtils.writeLines(file, result);
//        } catch (IOException e) {
//            logger.error("创建文件和写文件失败", e);
//        }

        // return "redirect:http://localhost:80/" + projectName + "/" + env + "/application.properties";

        try (ServletOutputStream outputStream = response.getOutputStream()) {
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("application.properties", "UTF-8"));
            outputStream.write(result.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
